<?php
defined('BASEPATH') or exit('No direct script access allowed');

class StatisticsTable2 extends AdminController
{

	function __construct()
	{
		parent::__construct();
		$this->sidebar = "Overview-Home";
		$this->load->model('Table2_model');
	}

	function Index()
	{
		$this->load->model('Admin_model');
		$result['dept_list'] = $this->Admin_model->query('select distinct dept_id,dept_name from db_admin where dept_id is not null');
		$this->load->view('admin/statisticsTable2', $result);
	}


	function Export($dept_id = false, $start_time, $end_time, $isExport = false)
	{

		// $params= array('start_time'=>$start_time,'end_time'=>$end_time,'dept_id'=>$dept_id);
		// $connar['where']=$this ->sqlEscape('db_table2.status=?? and db_table2.sub_time>?? and db_table2.sub_time<??',array(CommonStatus::UnDeleted[XPHEnum::Value],strtotime($start_time),strtotime($end_time)));
		// if($dept_id!=-1){
		// 	$dept_name=$this -> Admin_model -> get_single(array('dept_id'=>$dept_id));
		// 	$result['dept_name']=$dept_name['dept_name'];
		// 	$connar['where'].=$this->sqlEscape(' and db_admin.dept_id=??',array($dept_id));								
		// }else{
		// 	$result['dept_name']='';
		// }
		// $connar['items']="date_format(from_unixtime(db_table2.sub_time),'%Y-%m-%d') as day";
		// $connar['join']=array('db_admin','db_admin.id=db_table2.admin_id');
		// $connar['groupby']='day';

		// $data['ret']=$this -> Table2_model -> get_list_full($connar);
		// $result['start_time']=$start_time;
		// $result['end_time']=$end_time;
		// $result['data']=array();
		// foreach ($data['ret'] as $k => $v) {
		// 	if(!isset($result['data'][$k])){
		// 		$result['data'][$k]=array('time'=>$v['day'],'data'=>array());
		// 	}else{
		// 		array_push($result['data'][$k]['time'],array('time'=>array(date('Y-m-d',$v['day']))));
		// 	}
		// 	$connar1['items']='address,type,content,advice';
		// 	$connar1['where']=$this -> sqlEscape("date_format(from_unixtime(sub_time),'%Y-%m-%d')=?? and status=??",array($v['day'],CommonStatus::UnDeleted[XPHEnum::Value]));
		// 	$time_data=$this -> Table2_model->get_list_full($connar1);
		// 	foreach ($time_data as $k1 => $v1) {
		// 		array_push($result['data'][$k]['data'],array('address'=>$v1['address'],'type'=>$v1['type'],'content'=>$v1['content'],'advice'=>$v1['advice']));					
		// 	}

		// }
		// $data='';
		// $data.="<div class='content'><div><h3>教学巡(查)检报表【".$result['start_time']."至".$result['end_time']."】</h3></div>";
		// if($result['dept_name']!=''){
		// 	 	$data.='<div><h4>【'.$result['dept_name'].'】</h4></div>';
		// }
		// foreach ($result['data'] as $k => $v) {	
		// 	$data.= "<div class='item'>【".$v['time']."】<br>";
		// 	foreach ($v['data'] as $k1 => $v1) {
		// 		$num=$k+1;
		// 		$data.= $num.'.'.'【'.$v1['address'].'】【'.(new Table2Type)->show($v1['type']).'】【'.$v1['content'].'】【'.$v1['advice'].'】<br>';
		// 	}
		// 	$data.= "</div>";				
		// }
		// $data.= "</div>";
		$role = explode(',', getSess()['sub_role_id']);
		array_push($role, getSess()['role_id']);

		if (in_array(WebRole::Admin[XPHEnum::Value], $role)  || in_array(WebRole::Manage[XPHEnum::Value], $role) || in_array(WebRole::SchoolLeader[XPHEnum::Value], $role)) {
			// $dept_id=false;
		} else {
			$dept_id = getSess()['dept_id'];
		}
		$params = array('start_time' => $start_time, 'end_time' => $end_time, 'dept_id' => $dept_id);
		$connar['items'] = 'dept_id,dept_name';
		$connar['where'] = $this->sqlEscape('db_table2.status=?? and db_table2.sub_time>?? and db_table2.sub_time<??', array(CommonStatus::UnDeleted[XPHEnum::Value], strtotime($start_time), strtotime($end_time)));
		$connar['join'] = array('db_admin', 'db_admin.id=db_table2.admin_id');
		$connar['groupby'] = 'db_admin.dept_id';
		if ($dept_id != -1) {
			$connar['where'] .= $this->sqlEscape(" and db_admin.dept_id=??", array($dept_id));
		}
		$data = $this->Table2_model->get_list_full($connar);
		// foreach ($data as $k => $v) {
		$result = $data;
		// }
		foreach ($data as $k => $v) {
			$connar1['where'] = $this->sqlEscape('db_table2.status=?? and db_table2.sub_time>?? and db_table2.sub_time<?? and db_admin.dept_id=??', array(CommonStatus::UnDeleted[XPHEnum::Value], strtotime($start_time), strtotime($end_time), $v['dept_id']));
			$connar1['items'] = "date_format(from_unixtime(db_table2.sub_time),'%Y-%m-%d') as day,db_admin.dept_id dept_id";
			$connar1['join'] = array('db_admin', 'db_admin.id=db_table2.admin_id');
			$connar1['groupby'] = 'day';
			if ($dept_id != -1) {
				$connar1['where'] .= $this->sqlEscape(" and db_admin.dept_id=??", array($v['dept_id']));
			}
			$data['ret'] = $this->Table2_model->get_list_full($connar1);

			foreach ($data['ret'] as $k1 => $v1) {
				$result[$k]['data'][$k1] = array('time' => $v1['day'], 'data' => array());
				$connar2['items'] = 'address,type,content,advice';
				$connar2['join'] = array('db_admin', 'db_admin.id=db_table2.admin_id');
				$connar2['where'] = $this->sqlEscape("date_format(from_unixtime(db_table2.sub_time),'%Y-%m-%d')=?? and db_table2.status=?? and db_admin.dept_id=??", array($v1['day'], CommonStatus::UnDeleted[XPHEnum::Value], $v1['dept_id']));
				$time_data = $this->Table2_model->get_list_full($connar2);
				// echo $this->db->last_query();
				// echo "<br/>";
				// $result[$k]['data']['data']=array();
				foreach ($time_data as $k2 => $v2) {
					array_push($result[$k]['data'][$k1]['data'], array('address' => $v2['address'], 'type' => $v2['type'], 'content' => $v2['content'], 'advice' => $v2['advice']));
				}
			}
		}
		$result = array('time' => array('start_time' => $start_time, 'end_time' => $end_time), 'data' => $result);
		$data = '';
		$data .= "<div class='content'><div><h3>教学巡(查)检报表【" . $result['time']['start_time'] . "至" . $result['time']['end_time'] . "】</h3></div>";
		foreach ($result['data'] as $k => $v) {
			$data .= "<h5>" . $v['dept_name'] . "</h5>";
			foreach ($v['data'] as $k1 => $v1) {
				$data .= "<div class='item'>【" . $v['data'][$k1]['time'] . "】<br>";
				$num = 0;
				foreach ($v1['data'] as $k2 => $v2) {
					$num++;
					$data .= $num . '.' . '【' . $v2['address'] . '】【' . (new Table2Type)->show($v2['type']) . '】【' . $v2['content'] . '】【' . $v2['advice'] . '】<br>';
				}
			}
			$data .= '<br>';
		}
		$data .= '</div>';
		if (!$isExport) {
			echo $data;
			$this->load->view('admin/statisticsTable2Export', $params);
		} else {
			$this->load->library('PHPWord/PHPWord');
			$PHPWord = new PHPWord();
			$section = $PHPWord->createSection();
			$PHPWord->addFontStyle('rStyle', array('bold' => true, 'color' => '000000', 'size' => 16));
			$PHPWord->addParagraphStyle('pStyle', array('align' => 'center'));
			$section->addText("教学巡(查)检报表【" . $start_time . "至" . $end_time . "】", 'rStyle', 'pStyle');
			$PHPWord->addFontStyle('title1', array('bold' => true, 'color' => '000000', 'size' => 14));
			$section->addTextBreak(2);
			$PHPWord->addFontStyle('fStyle', array('bold' => false, 'color' => '000000', 'size' => 12));
			$num = 0;

			foreach ($result['data'] as $k => $v) {
				$section->addText($v['dept_name'], 'title1');
				$section->addTextBreak(1);
				foreach ($v['data'] as $k1 => $v1) {
					$section->addText("【" . $v1['time'] . "】");
					foreach ($v1['data'] as $k2 => $v2) {
						$num = $k1 + 1;
						$section->addText($num . '.' . '【' . $v2['address'] . '】【' . (new Table2Type)->show($v2['type']) . '】【' . $v2['content'] . '】【' . $v2['advice'] . '】');
					}
					$section->addTextBreak(1);
				}
				$section->addTextBreak(2);
			}


			$write = new PHPWord_Writer_Word2007($PHPWord);
			$director = "upload/export/" . date('Ymd', time()) . "/";
			if (!file_exists($director)) {
				//检查是否有该文件夹，如果没有就创建，并给予最高权限
				mkdir($director, 0777);
			}
			$file_name = '教学巡(查)检报表' . date('Ymd') . '.docx';
			$write->save($director . iconv('utf-8', 'gb2312', $file_name));
			exit(retJson('返回地址', true, $director . $file_name));
		}
	}
}
